package day12;

/*
 * Author: JacksonCoder
 * FileName: AtomicTest
 * Version: 1.0.0
 * Date: 2025/4/22
 */

import java.util.concurrent.atomic.AtomicInteger;

public class AtomicTest {
    /**
     * 使用 AtomicInteger
     * 启动两个线程
     * 最终输出 count = 20000
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        AtomicInteger count = new AtomicInteger(0);

        Runnable task = () -> {
            for (int i = 0; i < 10000; i++) {
                // 自增操作
                // TODO: 填空
                count.incrementAndGet();
            }
        };

        Thread t1 = new Thread(task);
        Thread t2 = new Thread(task);

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        System.out.println("最终结果: " + count.get()); // 应该是 20000
    }
}
